package com.yql.controller;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.PageInfo;
import com.yql.entity.Order;
import com.yql.entity.User;
import com.yql.entity.OrderVO;
import com.yql.service.OrderService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class OrderController {

    @Autowired
    OrderService orderService;
    @RequestMapping("index")
    public String index(@RequestParam(defaultValue = "list") String url, Model model){
        model.addAttribute("url",url);
        return "index";
    }

    @RequestMapping("list")
    public String list(OrderVO vo, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "2") Integer pageSize, Model model, HttpSession session){
        User user = (User) session.getAttribute("user");
        vo.setUid(user.getId());
        model.addAttribute("username",user.getUname());
        PageInfo<Order> info = orderService.listOrder(vo, pageNum, pageSize);
        model.addAttribute("info",info);
        model.addAttribute("vo",vo);
        return "list";
    }

    @RequestMapping("toAdd")
    public String toAdd(Model model,HttpSession session){
        Snowflake snowflake = IdUtil.getSnowflake(31, 31);
        Long orderId = snowflake.nextId();
        User user = (User) session.getAttribute("user");
        model.addAttribute("single",user.getUname());
        model.addAttribute("orderId",orderId);
        return "add";
    }
}
